//
//  AppDelegate.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/23.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit

var sharedAccount = UserAccount.loadAccount()
var noticeName = "JPNoticeSwitchVC"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultController()
        
        window?.makeKeyAndVisible()
        
        setupAppearance()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchVC:", name: noticeName, object: nil)
        
        return true
    }
    
    func switchVC(notice:NSNotification){
    
        let isMainVc = notice.object as! Bool
        
        window?.rootViewController = isMainVc ? MainController() : WelcomeController()
    }

    func defaultController()->UIViewController{
    
        if sharedAccount == nil{
        
            return MainController()
        }
        
        return  isUpated() ? NewFeaturesController(collectionViewLayout: UICollectionViewFlowLayout()) : WelcomeController()
    }
    
    func isUpated()->Bool{
    
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as!String
        
        let version = Double(currentVersion)
        
        let versionKey = "versionKey"
        
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        
        print(sandBoxVersion)
        
        NSUserDefaults.standardUserDefaults().setDouble(version!, forKey: "versionKey")
        
        return version > sandBoxVersion
    }

    // 在appDelegate中统一设置导航栏、tabBar的属性
    
    func setupAppearance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

